<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateShopOrderInvoiceTable extends Migrator
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('shop_order_invoice', ['comment' => '发票', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn(Column::enum('type', ['person', 'company'])->setNull(true)->setDefault(null)->setComment('发票类型:person=个人,company=企事业单位'))
            ->addColumn('order_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '订单'])
            ->addColumn('user_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '用户'])
            ->addColumn('name', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '名称'])
            ->addColumn('tax_no', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '税号'])
            ->addColumn('address', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '单位地址'])
            ->addColumn('mobile', 'string', ['limit' => 20, 'null' => true, 'default' => null, 'comment' => '手机号码'])
            ->addColumn('bank_name', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '开户银行'])
            ->addColumn('bank_no', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '银行账户'])
            ->addColumn('amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '金额'])
            ->addColumn('download_urls', 'string', ['limit' => 2500, 'null' => true, 'default' => null, 'comment' => '发票地址'])
            ->addColumn('invoice_amount', 'decimal', ['precision' => 10, 'scale' => 2, 'null' => true, 'default' => null, 'comment' => '开票金额'])
            ->addColumn(Column::enum('status', ['cancel', 'unpaid', 'waiting', 'finish'])->setDefault('unpaid')->setComment('状态:cancel=已取消,unpaid=未支付,waiting=等待处理,finish=已开具'))
            ->addColumn('finish_time', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '开具时间'])
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->addIndex('user_id')
            ->addIndex('order_id')
            ->create();
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        $table = $this->table('shop_order_invoice');
        $table->drop();
    }
}
